<?php


namespace app\store\controller\fitness;


use app\common\model\Fitness as FitnessModel;
use app\store\controller\Controller;
use think\Config;

class Physical extends Controller
{
    public function analyse_index($user_id){
        $model = new FitnessModel();
        $coachList = $this->coachList();
        $list = $model->getList(array_merge(['type'=>5],$this->request->param()));
        $fitness_type = Config::get('fitness_type');
        $title = "身体体测表";
        $add_url = "fitness.physical/analyse_add";
        $edit_url = "fitness.physical/analyse_edit";
        return $this->fetch('fitness/plan/index', compact('list','coachList','fitness_type','user_id','title','add_url','edit_url'));
    }
    /**
     * @title 身体体测添加数据
     * @param $user_id
     * @author Qian
     * Time: 2023/3/16  13:46
     */
    private $fat_config = [
        1=>'体重过瘦型',
        2=>'体重过轻型',
        3=>'正常',
        4=>'体脂肪肌肉型',
        5=>'过多脂肪型',
        6=>'肌肉过重型',
        7=>'轻微肥胖型',
        8=>'肥胖型',
        9=>'过度肥胖型',
    ];

    public function analyse_add($user_id){

        if (!$this->request->isAjax()){
            $coachList = $this->coachList();
            $fat_config = $this->fat_config;
            return $this->fetch('analyse_add',compact('coachList','fat_config'));
        }
        //添加数据
        $data = $this->postData('fitness');
        $model = new FitnessModel();
        if ($model->add($data,$user_id,5)){
            return  $this->renderSuccess('添加成功',url('fitness.physical/analyse_index',['user_id'=>$user_id]));
        }
        return $this->renderError($model->getError()?:'添加失败');
    }

    public function analyse_edit($id){

        $detail = FitnessModel::detail(['id'=>$id]);
        $fat_config = $this->fat_config;
        if (!$this->request->isAjax()){
            $coachList = $this->coachList();
            $info = json_decode($detail['data'],true);
            $info['user']['time'] = isset($info['user']['time'])?$info['user']['time']:'';
//            dump(json_decode($detail->toArray()['data'],true));die;
            return $this->fetch('analyse_edit',compact('coachList','detail','info','fat_config'));
        }
        //添加数据
        $data = $this->postData('fitness');
        if ($detail->edit($data)){
            return  $this->renderSuccess('保存成功',url('fitness.physical/analyse_index',['user_id'=>$detail['user_id']]));
        }
        return $this->renderError($detail->getError()?:'保存失败');
    }

    public function assess_index($user_id){
        $model = new FitnessModel();
        $coachList = $this->coachList();
        $list = $model->getList(array_merge(['type'=>4],$this->request->param()));
        $fitness_type = Config::get('fitness_type');
        $title = "运动能力评估表";
        $add_url = "fitness.physical/assess_add";
        $edit_url = "fitness.physical/assess_edit";
        return $this->fetch('fitness/plan/index', compact('list','coachList','fitness_type','user_id','title','add_url','edit_url'));
    }
    /**
     * @title 运动能力评估
     * @param $user_id
     * @author Qian
     * Time: 2023/3/16  13:58
     */
    public function assess_add($user_id){

        $standard = [
            5=>['name'=>'好','value'=>5],
            4=>['name'=>'较好','value'=>4],
            3=>['name'=>'中等','value'=>3],
            2=>['name'=>'较差','value'=>2],
            1=>['name'=>'极差','value'=>1],
        ];
        if (!$this->request->isAjax()){
            $coachList = $this->coachList();
            return $this->fetch('assess_add',compact('coachList','standard'));
        }
        //添加数据
        $data = $this->postData('fitness');
        $data['data']['user'] = $data['user'];
        $data['data']['testing'] = $data['testing'];
        unset($data['user']);
        unset($data['testing']);
        $model = new FitnessModel();
        if ($model->add($data,$user_id,4)){
            return  $this->renderSuccess('添加成功',url('fitness.physical/assess_index',['user_id'=>$user_id]));
        }
        return $this->renderError($model->getError()?:'添加失败');
    }

    public function assess_edit($id){

        $standard = [
            5=>['name'=>'好','value'=>5],
            4=>['name'=>'较好','value'=>4],
            3=>['name'=>'中等','value'=>3],
            2=>['name'=>'较差','value'=>2],
            1=>['name'=>'极差','value'=>1],
        ];
        $detail = FitnessModel::detail(['id'=>$id]);
        if (!$this->request->isAjax()){
            $coachList = $this->coachList();
            $fitness = json_decode($detail['data'],true);
            return $this->fetch('assess_edit',compact('coachList','standard','detail','fitness'));
        }
        //添加数据
        $data = $this->postData('fitness');
        $data['data']['user'] = $data['user'];
        $data['data']['testing'] = $data['testing'];
        unset($data['user']);
        unset($data['testing']);
        if ($detail->edit($data)){
            return  $this->renderSuccess('保存成功',url('fitness.physical/assess_index',['user_id'=>$detail['user_id']]));
        }
        return $this->renderError($detail->getError()?:'保存失败');
    }

    public function coachList(){
        //所属门店的教练
        $plan = new Plan();
        return $plan->coachList();
    }

}